package com.xmall.product.controller;

import com.xmall.annotation.LoginRequiredFalse;
import com.xmall.aspect.OperateRecord;
import com.xmall.aspect.RepeatSubmit;
import com.xmall.define.base.Result;
import com.xmall.define.dict.OperateTypeEnum;
import com.xmall.define.dto.product.EsBaseAttrDto;
import com.xmall.product.define.dto.baseAttr.BaseAttrDto;
import com.xmall.product.define.request.attr.BaseAttrQueryParams;
import com.xmall.product.define.request.attr.BaseAttrUpdateRequest;
import com.xmall.product.service.IBaseAttrService;
import com.xmall.validate.group.AddGroup;
import com.xmall.validate.group.UpdateGroup;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 属性
 */
@RestController
@RequestMapping("/service/product/baseAttr")
public class BaseAttrController {

    @Resource
    private IBaseAttrService baseAttrService;

    /**
     * 构造es基本属性对象
     */
    @LoginRequiredFalse
    @PostMapping("/buildEsBaseAttr")
    public Result<List<EsBaseAttrDto>> buildEsBaseAttr(@RequestBody List<Long> attrIds) {
        return Result.success(baseAttrService.buildEsBaseAttr(attrIds));
    }

    /**
     * 属性列表
     */
    @GetMapping("/list/{groupId}")
    public Result<List<BaseAttrDto>> list(@PathVariable("groupId") Long groupId){
        List<BaseAttrDto> list = baseAttrService.listWithCascade(new BaseAttrQueryParams().setGroupId(groupId));
        return null != list ? Result.success(list) : Result.error();
    }

    /**
     * 详情
     */
    @GetMapping("/{id}")
    public Result<BaseAttrDto> info(@PathVariable("id") Long id){
        BaseAttrDto attrDto = baseAttrService.infoWithCascade(id);
        return null != attrDto ? Result.success(attrDto) : Result.error();
    }

    /**
     * 新增
     */
    @OperateRecord(type = OperateTypeEnum.ADD)
    @RepeatSubmit
    @PreAuthorize("hasAuthority('product:baseAttr:save')")
    @PostMapping("/save")
    public Result save(@Validated(value = AddGroup.class) @RequestBody BaseAttrUpdateRequest request){
        baseAttrService.saveEntity(request);
        return Result.success();
    }

    /**
     * 编辑
     */
    @OperateRecord(type = OperateTypeEnum.UPDATE)
    @PreAuthorize("hasAuthority('product:baseAttr:update')")
    @PostMapping("/update")
    public Result update(@Validated(value = UpdateGroup.class) @RequestBody BaseAttrUpdateRequest request){
        baseAttrService.updateEntity(request);
        return Result.success();
    }

    /**
     * 删除
     */
    @OperateRecord(type = OperateTypeEnum.DELETE)
    @PreAuthorize("hasAuthority('product:baseAttr:delete')")
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Long id){
        baseAttrService.deleteEntity(id);
        return Result.success();
    }

    /**
     * 交换排序值
     */
    @OperateRecord(type = OperateTypeEnum.UPDATE)
    @PostMapping("/swapSort/{prevAttrId}/{nextAttrId}")
    public Result swapSort(@PathVariable("prevAttrId") Long prevAttrId,
                           @PathVariable("nextAttrId") Long nextAttrId){
        baseAttrService.swapSort(prevAttrId, nextAttrId);
        return Result.success();
    }
}
